$(document).ready(function() {
	var $allCheck = $("#allCheck");// 全选复选框
	var $idsCheck = $('[name=ids]:checkbox');// ID复选框
	var $deleteButton = $("#deleteButton");// 删除按钮
	var $searchButton = $("#searchButton");// 查询按钮
	
    var $enableButton = $("#enableButton");// 启用按钮
    var $disableButton = $("#disableButton");// 禁用按钮
    
	$allCheck.checkAll();
	// 全选
	$allCheck.click(function() {
		$allCheck.checkAll();
		var isChecked = $allCheck.is(":checked");
		if (isChecked == false) {
			$deleteButton.prop("disabled", true);
			$enableButton.prop("disabled", true);
	        $disableButton.prop("disabled", true);
		} else {
			$deleteButton.prop("disabled", false);
            $enableButton.prop("disabled", false);
            $disableButton.prop("disabled", false);
		}
	});

	// 无复选框被选中时,删除按钮不可用
	$idsCheck.click(function() {
		var $idsChecked = $("[name='ids']:checked");
		if ($idsChecked.size() > 0) {
			$deleteButton.prop("disabled", false);
            $enableButton.prop("disabled", false);
            $disableButton.prop("disabled", false);
		} else {
			$deleteButton.prop("disabled", true);
            $enableButton.prop("disabled", true);
            $disableButton.prop("disabled", true);
		}
	});

	// 查找
	$searchButton.click(function() {
		// $pageNumber.val("1");
		// $listForm.submit();
		$("#listForm").submit();
	});

});

// 批量删除
function deleteAll(url, id) {
	var $idsCheckedCheck = $("[name='ids']:checked");
	var $deleteButton = $("#deleteButton");// 删除按钮
	var ids = $idsCheckedCheck.serialize();
	if (confirm('您确定要删除吗？') == true) {
		if (id == undefined) {
			$.ajax({
				url : url,
				data : ids,
				dataType : "json",
				async : false,
				beforeSend : function(data) {
					$deleteButton.prop("disabled", true)
				},
				success : function(data) {
					$deleteButton.prop("disabled", false)
					if (data.status == "success") {
						$idsCheckedCheck.parent().parent().remove();
					}
					sendMessage({
						title : "删除!",
						text : data.message,
						type : data.status,
						timer : 2000,
						showConfirmButton : false
					});
					// $.message(data.status, data.message);
					location.reload(true);
				}
			});
		} else {
			$.ajax({
				url : url,
				data : 'id=' + id,
				dataType : "json",
				async : false,
				beforeSend : function(data) {
					$deleteButton.prop("disabled", true)
				},
				success : function(data) {
					$deleteButton.prop("disabled", false)
					if (data.status == "success") {
						$idsCheckedCheck.parent().parent().remove();
					}
					sendMessage({
						title : "删除!",
						text : data.message,
						type : data.status,
						timer : 2000,
						showConfirmButton : false
					});
					// $.message(data.status, data.message);
					location.reload(true);
				}
			});
		}

	}
}

function enableAll(url){
    batchProcess($("#enableButton"), url, '启用','启用');
}

function disableAll(url){
    batchProcess($("#disableButton"), url, '禁用','禁用');
}  

function batchProcess(btnObj, url, actinoName, value) {
    var $idsCheckedCheck = $("[name='ids']:checked");
    var $deleteButton = btnObj;// 删除按钮
    var ids = $idsCheckedCheck.serialize();
    if (confirm('您确定要'+actinoName+'吗？') == true) {       
        $.ajax({
            url : url,
            data : ids,
            dataType : "json",
            async : false,
            beforeSend : function(data) {
                $deleteButton.prop("disabled", true);
            },
            success : function(data) {
                $deleteButton.prop("disabled", false);
                if (data.status == "success") {
                    $idsCheckedCheck.parent().parent().find('#enabled').html(value);
                    if(actinoName == '禁用'){                        
                        $idsCheckedCheck.parent().parent().find('#operated').find('#edit-div').removeClass('am-hide');                       
                    }else if(actinoName =='启用'){
                        $idsCheckedCheck.parent().parent().find('#operated').find('#edit-div').addClass('am-hide');
                    }
                }
                sendMessage({
                    title : actinoName,
                    text : data.message,
                    type : "success",
                    timer : 2000,
                    showConfirmButton : false
                });                
            }
        });
    }
}

function promptModalWin(id) {
	var $id = $('#' + id) || {};
	var $doc;
	if (window.location != window.parent.location) {
		$doc = window.parent.jQuery.noConflict();
	} else {
		$doc = jQuery.noConflict();
	}
	$doc("#modal").html($id.html());
	alert($doc("#modal").html());
	$doc("#modal").modal({
		relatedTarget : this,
		onConfirm : function(e) {
			alert('你输入的是：' + e.data || '')
		},
		onCancel : function(e) {
			alert('不想说!');
		}
	});
}

function ajaxUploadFile(id, url) {
    if($('#modal')){
        $('#modal').modal('open');
    }


    $.ajaxFileUpload({
        url : url,
        secureuri : false,
        fileElementId : id,// file标签的id
        dataType : 'json',// 返回数据的类型
        success : function(data, status) {
        
            if($('#modal')){
                $('#modal').modal('close');
            }
            
            if (typeof (data.error) != 'undefined') {
                alert(data.error);
            } else {
                sendMessage({
                    title : (data.status==0)?"上传失败!":"上传完成!",
                    text : data.result,
                    type : "success",
                    confirmButtonText : "确认"
                });
                location.reload();
            }
        },
        error : function(data, status, e) {
            if($('#modal')){
                $('#modal').modal('close');
            }
            alert(e);
        }
    });
}

function sendMessage(msg) {
	if ('parentIFrame' in window) {
		window.parentIFrame.sendMessage(msg);
	}
	return false;
}
